'\" te
.\" Copyright 1989 AT&T
.\" Copyright 2000, Sun Microsystems, Inc.  All Rights Reserved
.\" Copyright 2022, Oxide Computer Company
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH GETOPT 1 "Jun 1, 2022"
.SH NAME
getopt \- parse command options
.SH SYNOPSIS
.nf
\fBset\fR \fB--\fR ` getopt \fIoptstring\fR $ * `
.fi

.SH DESCRIPTION
The \fBgetopts\fR command supersedes \fBgetopt\fR. For more information, see
NOTES below.
.sp
.LP
\fBgetopt\fR is used to break up options in command lines for easy parsing by
shell procedures and to check for legal options. \fIoptstring\fR is a string of
recognized option letters; see \fBgetopt\fR(3C). If a letter is followed by a
colon (\fB:\fR), the option is expected to have an argument which may or may
not be separated from it by white space. The special option \fB-\fR is used to
delimit the end of the options. If it is used explicitly, \fBgetopt\fR
recognizes it; otherwise, \fBgetopt\fR generates it; in either case,
\fBgetopt\fR places it at the end of the options. The positional parameters
(\fB$1 $2\fR .\|.\|.\|) of the shell are reset so that each option is preceded
by a \fB\(mi\fR and is in its own positional parameter; each option argument is
also parsed into its own positional parameter.
.SH EXAMPLES
\fBExample 1 \fRProcessing the arguments for a command
.sp
.LP
The following code fragment shows how one might process the arguments for a
command that can take the options \fB-a\fR or \fB-b\fR, as well as the option
\fB-o\fR, which requires an argument:

.sp
.in +2
.nf
\fBset -- `getopt abo: $*`
if [ $? != 0 ]
then
           echo $USAGE
           exit 2
fi
for i in $*
do
           case $i in
           -a | -b)     FLAG=$i; shift;;
           -o)           OARG=$2; shift 2;;
           --)           shift; break;;
           esac
done\fR
.fi
.in -2
.sp

.sp
.LP
This code accepts any of the following as equivalent:

.sp
.in +2
.nf
\fBcmd -aoarg filename1 filename2
cmd -a -o arg filename1 filename2
cmd -oarg -a filename1 filename2
cmd -a -oarg -- filename1 filename2\fR
.fi
.in -2
.sp

.SH ATTRIBUTES
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
CSI	enabled
.TE

.SH SEE ALSO
.BR Intro (1),
.BR getoptcvt (1),
.BR getopts (1),
.BR sh (1),
.BR shell_builtins (1),
.BR getopt (3C),
.BR attributes (7)
.SH DIAGNOSTICS
\fBgetopt\fR prints an error message on the standard error when it encounters
an option letter not included in \fIoptstring\fR.
.SH NOTES
Reset \fBoptind\fR to \fB1\fR when rescanning the options.
.sp
.LP
\fBgetopt\fR does not support the part of Rule 8 of the command syntax standard
(see \fBIntro\fR(1)) that permits groups of option-arguments following an
option to be separated by white space and quoted. For example,
.sp
.in +2
.nf
\fBcmd -a -b -o "xxx z yy" filename\fR
.fi
.in -2
.sp

.sp
.LP
is not handled correctly. To correct this deficiency, use the \fBgetopts\fR
command in place of \fBgetopt\fR.
.sp
.LP
If an option that takes an option-argument is followed by a value that is the
same as one of the options listed in \fIoptstring\fR (referring to the earlier
EXAMPLES section, but using the following command line:
.sp
.in +2
.nf
\fBcmd -o -a filename\fR
.fi
.in -2
.sp

.sp
.LP
\fBgetopt\fR always treats it as an option-argument to \fB-o\fR; it never
recognizes \fB-a\fR as an option. For this case, the \fBfor\fR loop in the
example shifts past the \fIfilename\fR argument.
